<template>
  <div class="login_box">
    <!-- =================== 登录页 =================== -->
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="loginFormRef"
      class="demo-ruleForm"
    >
      <!-- 名字 -->
      <el-form-item prop="username">
        <el-input
          v-model="ruleForm.username"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user-solid"
        ></el-input>
      </el-form-item>
      <!-- 密码 -->
      <el-form-item prop="password">
        <el-input
          v-model="ruleForm.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-user-solid"
          >></el-input
        >
      </el-form-item>
      <!-- 按钮 -->
      <el-form-item class="login_Btn">
        <el-button type="primary" @click="submitForm">登录</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>

      <!-- login 图片 -->
      <img src="../../assets/login.jpg" alt="" class="login_img" />
    </el-form>
  </div>
</template>

<script>
import { LoginApi } from "../../http/api";
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      //   校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //   登录
    submitForm() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return false;
        const res = await LoginApi(this.ruleForm);
        // console.log("登录了吗？", res);
        // console.log("ok");
        // if (res.id) {
        this.$router.push("/home");
        // }
      });
    },

    //   重置
    resetForm() {
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>

<style lang="scss" scoped>
.login_box {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  .demo-ruleForm {
    position: relative;
    box-sizing: border-box;
    padding: 100px 20px 20px;
    width: 35%;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    text-align: center;
  }
  //   按钮
  .login_Btn {
    float: right;
  }
  //   login 图片
  .login_img {
    box-sizing: border-box;
    position: absolute;
    top: -80px;
    width: 140px;
    padding: 10px;
    height: 140px;
    border-radius: 50%;
    background-color: #fff;
  }
}
</style>
